package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.CategoryDo;
import org.checkerframework.checker.units.qual.C;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-19 23:17
 */
public interface CategoryMapper extends BaseMapper<CategoryDo> {
    default CategoryDo selectByName(String categoryName) {
        LambdaQueryWrapper<CategoryDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CategoryDo::getName, categoryName);

        //查询
        return selectOne(wrapper);
    }

    //分页查询
    default Page<CategoryDo> selectPageList(Long current, Long size, String categoryName, LocalDate startDate, LocalDate endDate) {
        //创建页面
        Page<CategoryDo> page = new Page<>(current, size);

        //创建查询条件
        LambdaQueryWrapper<CategoryDo> wrapper = new LambdaQueryWrapper<>();

        wrapper
                .like(Objects.nonNull(categoryName), CategoryDo::getName, categoryName)
                .ge(Objects.nonNull(startDate), CategoryDo::getCreateTime, startDate)
                .le(Objects.nonNull(endDate), CategoryDo::getCreateTime, endDate)
                .orderByDesc(CategoryDo::getCreateTime);

        //分页查询
        return selectPage(page, wrapper);
    }

    // limit查询
    default List<CategoryDo> selectByLimit(Long size) {
        return selectList(Wrappers.<CategoryDo>lambdaQuery()
                .orderByDesc(CategoryDo::getCreateTime)
                .last(String.format("LIMIT %d", size)));
    }
}
